import 'dart:convert';
/// amount : 0
/// createTime : "2024-01-31T08:35:59.042Z"
/// fullPay : true
/// id : 0
/// isDelete : true
/// itemList : [{"count":0,"id":0,"isDelete":true,"mark":"string","material":"string","name":"string","price":0,"sellId":0,"size":"string","standards":"string"}]
/// payAmount : 0
/// realAmount : 0
/// recordTime : "2024-01-31T08:35:59.042Z"
/// sellPhone : "string"
/// sellUserName : "string"
/// userId : 0

SalesEntity salesEntityFromJson(String str) => SalesEntity.fromJson(json.decode(str));
String salesEntityToJson(SalesEntity data) => json.encode(data.toJson());
class SalesEntity {
  SalesEntity({
      this.amount, 
      this.createTime, 
      this.fullPay = false,
      this.id, 
      this.isDelete, 
      this.itemList, 
      this.payAmount, 
      this.realAmount, 
      this.recordTime, 
      this.sellPhone, 
      this.sellUserName, 
      this.userId,});

  SalesEntity.fromJson(dynamic json) {
    amount = json['amount'];
    createTime = json['createTime'];
    fullPay = json['fullPay'] ?? false;
    id = json['id'];
    isDelete = json['isDelete'];
    if (json['itemList'] != null) {
      itemList = [];
      json['itemList'].forEach((v) {
        itemList?.add(SalesItem.fromJson(v));
      });
    }
    payAmount = json['payAmount'];
    realAmount = json['realAmount'];
    recordTime = json['recordTime'];
    sellPhone = json['sellPhone'];
    sellUserName = json['sellUserName'];
    userId = json['userId'];
  }
  num? amount;
  String? createTime;
  bool fullPay = false;
  num? id;
  bool? isDelete;
  List<SalesItem>? itemList;
  num? payAmount;
  num? realAmount;
  String? recordTime;
  String? sellPhone;
  String? sellUserName;
  num? userId;

  num get totalCount {
    if (itemList == null || itemList!.isEmpty) {
      return 0;
    }
    num totalCount = 0;
    for (SalesItem item in itemList!) {
      if (item.count != null) {
        totalCount += item.count!;
      }
    }
    return totalCount;
  }

  num get totalAmount {
    if(realAmount != null) {
      return realAmount!;
    }
    if (amount != null) {
      return amount!;
    }
    if (itemList == null || itemList!.isEmpty) {
      return 0;
    }
    num totalAmount = 0;
    for (SalesItem item in itemList!) {
      if (item.count != null && item.price != null) {
        totalAmount += item.count! * item.price!;
      }
    }
    return totalAmount;
  }


SalesEntity copyWith({  num? amount,
  String? createTime,
  bool? fullPay,
  num? id,
  bool? isDelete,
  List<SalesItem>? itemList,
  num? payAmount,
  num? realAmount,
  String? recordTime,
  String? sellPhone,
  String? sellUserName,
  num? userId,
}) => SalesEntity(  amount: amount ?? this.amount,
  createTime: createTime ?? this.createTime,
  fullPay: fullPay ?? this.fullPay,
  id: id ?? this.id,
  isDelete: isDelete ?? this.isDelete,
  itemList: itemList ?? this.itemList,
  payAmount: payAmount ?? this.payAmount,
  realAmount: realAmount ?? this.realAmount,
  recordTime: recordTime ?? this.recordTime,
  sellPhone: sellPhone ?? this.sellPhone,
  sellUserName: sellUserName ?? this.sellUserName,
  userId: userId ?? this.userId,
);
  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['amount'] = amount;
    map['createTime'] = createTime;
    map['fullPay'] = fullPay;
    map['id'] = id;
    map['isDelete'] = isDelete;
    if (itemList != null) {
      map['itemList'] = itemList?.map((v) => v.toJson()).toList();
    }
    map['payAmount'] = payAmount;
    map['realAmount'] = realAmount;
    map['recordTime'] = recordTime;
    map['sellPhone'] = sellPhone;
    map['sellUserName'] = sellUserName;
    map['userId'] = userId;
    return map;
  }

}

/// count : 0
/// id : 0
/// isDelete : true
/// mark : "string"
/// material : "string"
/// name : "string"
/// price : 0
/// sellId : 0
/// size : "string"
/// standards : "string"

SalesItem itemListFromJson(String str) => SalesItem.fromJson(json.decode(str));
String itemListToJson(SalesItem data) => json.encode(data.toJson());
class SalesItem {
  SalesItem({
      this.count, 
      this.id, 
      this.isDelete, 
      this.mark, 
      this.material, 
      this.name, 
      this.price, 
      this.sellId, 
      this.size, 
      this.standards,});

  SalesItem.fromJson(dynamic json) {
    count = json['count'];
    id = json['id'];
    isDelete = json['isDelete'];
    mark = json['mark'];
    material = json['material'];
    name = json['name'];
    price = json['price'];
    sellId = json['sellId'];
    size = json['size'];
    standards = json['standards'];
  }
  num? count;
  num? id;
  bool? isDelete;
  String? mark;
  String? material;
  String? name;
  num? price;
  num? sellId;
  String? size;
  String? standards;
SalesItem copyWith({  num? count,
  num? id,
  bool? isDelete,
  String? mark,
  String? material,
  String? name,
  num? price,
  num? sellId,
  String? size,
  String? standards,
}) => SalesItem(  count: count ?? this.count,
  id: id ?? this.id,
  isDelete: isDelete ?? this.isDelete,
  mark: mark ?? this.mark,
  material: material ?? this.material,
  name: name ?? this.name,
  price: price ?? this.price,
  sellId: sellId ?? this.sellId,
  size: size ?? this.size,
  standards: standards ?? this.standards,
);
  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['count'] = count;
    map['id'] = id;
    map['isDelete'] = isDelete;
    map['mark'] = mark;
    map['material'] = material;
    map['name'] = name;
    map['price'] = price;
    map['sellId'] = sellId;
    map['size'] = size;
    map['standards'] = standards;
    return map;
  }

}